class ProjectsController < ApplicationController

  
  def index
    @title="测试列表"
    @projects = Project.paginate :page => params[:page], :per_page => 200,:order => "costcenter,created_at DESC"
    @projects_costcenter = @projects.group_by {|c| c.costcenter.strip}
    

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @projects }
    end
  end


  def show
    @title="测试详细页"
    find_params
    find_point
    @users = @project.users
    @user2s = User.search(params[:search],params[:page]) if params[:search]
    @user_size=@users.size
    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @project }
    end
  end


  def new
    @title="新增一项测试"
    @project = Project.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @project }
    end
  end


  def edit
    @title="修改测试信息"
    @project = Project.find(params[:id])
  end


  def create
    @project = Project.new(params[:project])
    @project.costcenter.strip
    if @project.save
      #      flash[:notice] = 'A test was successfully created.'
     # @projects = Project.paginate :page => params[:page], :per_page => 20,:order => "costcenter,created_at DESC"
     # @projects_costcenter = @projects.group_by {|c| c.costcenter}
     # last_id = Project.find(:first,:order =>"id DESC").id
      
     # render :update do |page|
     #   page.replace_html "projects_table",:partial => "projects_list" 
     #   page.visual_effect(:highlight, "tr_#{last_id}")
     #   page.hide "new_project_form","reset_form"
     #   
     # end
     redirect_to("/projects")
    else
      render :action => "new",:controller => 'projects'
    end
 
  end


  def update
    @title="测试更新页"
    @project = Project.find(params[:id])

    respond_to do |format|
      
      if @project.update_attributes(params[:project])
        @project.costcenter.strip
        @project.save
        flash[:notice] = 'The test was successfully updated.'
        format.html { redirect_to(@project) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @project.errors, :status => :unprocessable_entity }
      end
    end
  end


  def destroy
    @project = Project.find(params[:id])
    @project.destroy
    flash[:notice] = 'The test was successfully deleted.'

    respond_to do |format|
      format.html { redirect_to(projects_url) }
      format.xml  { head :ok }
    end
  end
  
 
  def search_users
    project_id= params[:project_id]
    search = params[:search]
    @users=[]
    
    @project = Project.find(project_id)
    search2=search.split(%r(,|;|:|，|：|；))

    search2.each do |s|
      @users |= User.search4(s)
    end
    #    @users.uniq!
         
    render :update do |page|
      page.replace_html  'search_user_result', :partial =>"search_user_list"
    end

  end
  
  def add_users
    find_params
  #  point =params[:point]  
   
    @user_ids = params[:user_ids]
    @user_ids.each do |user|
      @project.projectusers.create(:user_id => user,:point => @project.per_point)
    end
    
  #  pu=Projectuser.create(:project_id => @project_id,:user_id =>@user_id,:point =>point)

    find_point
   
    #  @user =User.find(user_id)
    #    a user can be added to a project only once 
    #   unless @project.users.include?(@user)
    #      pu.save!
    #     @project.users << @user
    #     flash[:notice2] = "add a user successful"
    #   else
    #     flash[:notice2] = "the user exists"
    #  end
    
    @users = @project.users
    @user_size=@users.size

    render :update do |page|
      page.replace_html "users2_list", :partial =>"userlist"
    #  page.visual_effect(:highlight,@user_id,:duration => 3 )
    #  page.hide "img_#{@user_id}"
      page.replace_html "summy", :partial =>"show_point" 
      page.visual_effect :highlight,"summy",:duration =>2
      page['submit_search'].click
      #      page.replace_html 'notice2',flash[:notice2]
      #      page.show 'notice2'
      #      
      #      page.visual_effect(:highlight, "notice2",:duration => 3)
      #      page.delay(3) do
      #        page.replace_html 'notice2',""
      #        page.visual_effect(:fade, "notice2")
      #      end
      
    end
  end
  
  def delete_users
    find_params
    @user =User.find(@user_id)
    #    a user can be added to a project only once 
    @project.users.delete @user
    find_point
    @users = @project.users
    @user_size=@users.size

    render :update do |page|
      page.replace_html "users2_list", :partial =>"userlist"
      page.replace_html "summy", :partial =>"show_point" 
      page.visual_effect :highlight,"summy",:duration =>2

      
      #      page.show "img_#{user_id}" if page["img_#{user_id}"]
       
      #  page.visual_effect(:highlight,user_id )
      
    end
  end
  
  def delete_allusers
    find_params
    @project.projectusers.destroy_all
    find_point
    @users = @project.users
    @user_size=@users.size

    render :update do |page|
      page.replace_html "users2_list", :partial =>"userlist"
      page.replace_html "summy", :partial =>"show_point" 
      page.visual_effect :highlight,"summy",:duration =>2

      
      #      page.show "img_#{user_id}" if page["img_#{user_id}"]
       
      #  page.visual_effect(:highlight,user_id )
      
    end
  end

  def edit_point
    find_params
    project_id = params[:id]
    user_id=params[:user_id]
    point=params[:point]
    pu=Projectuser.find_by_project_id_and_user_id(project_id,user_id)
    pu.update_attributes(:point => point)
    
    find_point

    @users = @project.users
    @user_size=@users.size


    render :update do |page|
      page.replace_html "div_#{user_id}","<input type='text' value= '#{point}' size='10' name='point' id='point_#{user_id}'/>
      <input type='image' src='/images/save.jpeg' name='submit' id='submit'/>"
      page.visual_effect :highlight,"point_#{user_id}",:duration =>2
      
      page.replace_html "summy", :partial =>"show_point" 
      page.visual_effect :highlight,"summy",:duration =>2

    end
    
  end

  private

  def find_params
    @project_id = params[:id]
    @user_id ||= params[:user_id]
    @project = Project.find(@project_id)

    @point=@project.per_point

  end

  def find_point
    project_id = params[:id]
    @point_max = Projectuser.max(project_id)
    @point_min =  Projectuser.min(project_id)
    @point_sum =  Projectuser.total(project_id)
    @point_avg =  Projectuser.avg(project_id)

  end
  	
  
end
